<?php

declare(strict_types=1);

namespace App\Middleware\Auth;

use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
use Hyperf\Utils\Context;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class FooMiddleware implements MiddlewareInterface
{
    /**
     * @var ContainerInterface
     */
    protected $container;
    /**
     * @var RequestInterface
     */
    protected $request;
    /**
     * @var HttpResponse
     */
    protected $response;

    public function __construct(ContainerInterface $container, HttpResponse $response, RequestInterface $request)
    {
        $this->container = $container;
        $this->response  = $response;
        $this->request   = $request;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $request = $request->withAttribute("add_value",123);
        Context::set(ServerRequestInterface::class, $request);
        // $response = \Hyperf\Utils\Context::set(ResponseInterface::class, $response);
        //根据具体业务判断逻辑走向，这里假设用户携带的token有效
        $isValidToken = true;
        if($isValidToken) {
            return $handler->handle($request);
        }
        return $this->response->json(
            [
                'code'=> -1,
                'data'=> [
                    'error'=> '中间里验证token无效，阻止继续向下执行'
                ]
            ]
        );

    }
}